package com.budayi.service.impl;

import com.budayi.common.mapper.BaseMapper;
import com.budayi.common.service.impl.CoreServiceImpl;
import com.budayi.common.utils.ObjectUtils;
import com.budayi.entity.Menu;

import com.budayi.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author LIBOFEI
 * @ClassName MenuServiceImpl
 * @description 菜单实现
 * @date 2021/4/27 13:57
 * @Version 1.0
 **/
@Service
public class MenuServiceImpl extends CoreServiceImpl<Menu> implements MenuService {


    @Autowired(required = false)
    public MenuServiceImpl(BaseMapper<Menu> menuMapper) {
        super(menuMapper, Menu.class);
    }


    @Override
    public List<Menu> queryMenuByParent(Menu menu) {
        List<Menu> menus = super.queryObjectListByCondition(menu);
        if(ObjectUtils.isNotNull(menus)){
            Map<Long,Menu> menuMap = new LinkedHashMap<>();
            menus.forEach(one -> {
                if(one.getParentId() == 0){
                    menuMap.put(one.getId(),one);
                }
            });
            menus.forEach(two -> {
                if(two.getParentId() != 0){
                    if(ObjectUtils.isNotNull(menuMap.get(two.getParentId()))){
                        Menu one = menuMap.get(two.getParentId());
                        if(ObjectUtils.isNull(one.getChildMenuList())){
                            one.setChildMenuList(new ArrayList<>());
                        }
                        one.getChildMenuList().add(two);
                    }
                }
            });
            return new ArrayList<>(menuMap.values());
        }
        return null;
    }
}
